﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace publicLibrary
{
    public class paging<T>
    {
        IEnumerable<T> listObj;

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int pageSize;

        /// <summary>
        /// 当前页码
        /// </summary>
        public int pageIndex;

        public paging(IEnumerable<T> listObj, int pageSize = 20)
        {
            if (pageSize < 1) throw new Exception("每页显示的记录数不能小于1");
            this.listObj = listObj;
            this.pageSize = pageSize;
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int itemCount
        {
            get
            {
                return listObj.Count();
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount
        {
            get
            {
                if (itemCount % pageSize == 0) return itemCount / pageSize;
                return itemCount / pageSize + 1;
            }
        }

        /// <summary>
        /// 得到内容
        /// </summary>
        /// <param name="pageIndex">以1开始</param>
        /// <returns></returns>
        public IEnumerable<T> content(int pageIndex = 1)
        {
            this.pageIndex = pageIndex;
            if (pageIndex < 1) this.pageIndex = 1;
            if (pageIndex > pageCount) this.pageIndex = pageCount;
            int skipNum = (this.pageIndex - 1) * pageSize;
            return listObj.Skip(skipNum).Take(pageSize);
        }

        /// <summary>
        /// 返回分页字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<pagingInfoEnum, int> info()
        {
            Dictionary<pagingInfoEnum, int> pagingInfo = new Dictionary<pagingInfoEnum, int>();

            pagingInfo[pagingInfoEnum.pageIndex] = pageIndex;
            pagingInfo[pagingInfoEnum.itemCount] = itemCount;
            pagingInfo[pagingInfoEnum.pageCount] = pageCount;
            pagingInfo[pagingInfoEnum.pageSize] = pageSize;

            return pagingInfo;
        }

        /// <summary>
        /// 返回分页信息
        /// </summary>
        /// <param name="pagingInfo">分页信息的类型枚举</param>
        /// <returns></returns>
        public int info(pagingInfoEnum pagingInfo)
        {
            return info()[pagingInfo];
        }

    }

    public enum pagingInfoEnum
    {
        itemCount,
        pageCount,
        pageSize,
        pageIndex
    }
    public class pageNav {
        public int pageIndex { set; get; }
        public int pageCount { set; get; }
        public int pageSize { set; get; }
    }

}
